﻿using Microsoft.AspNetCore.Mvc;
using ProsonUser.DomainOrderInfo;
using ProsonUser.ProsonOrderDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;


 

namespace ProsonUser.ProsonOrder
{
    public class  POrder : ApplicationService, IOrder
    {
        private readonly IRepository<OrderInfo, int> _order;
        public POrder(IRepository<OrderInfo, int> users)
        {
            _order = users;
             
        }

      /// <summary>
      /// 添加订单
      /// </summary>
      /// <param name="dto"></param>
      /// <returns></returns>
        [HttpPost, Route("OrderAdd")]
        public async Task<int> AddOrder(OrderInfo dto)
        {
            var lst = await _order.InsertAsync(dto);
            dto.OrderInfoTime = DateTime.Now;
            if (lst != null)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> AddOrderServie(OrderCreateDTO dto)
        {
            var query =await _order.InsertAsync(ObjectMapper.Map<OrderCreateDTO,OrderInfo>(dto));
            dto.OrderInfoTime = DateTime.Now;
            if (query != null)
            {
             return 1;
            }
            else
            {
                return 0
                      ;
            }
           
        }
        [HttpGet,Route("GetOrderMessages")]
        public async Task<OrderInfo> GetOrderMessages(int id)
        {
            OrderInfo obj = await _order.FindAsync(id);
            return obj;
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete,Route("RemoveOrder")]
        public async Task<int> RemoveOrder(int id)
        {   
            var query = _order.Where(s=>s.Id.Equals(id)).FirstOrDefault();
            await  _order.DeleteAsync(query);
            return 1;
           
            

        }
        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="orderInfoName"></param>
        /// <returns></returns>
        [HttpGet,Route("Theorderlist")]
        public async Task<List<OrderDTO>> Theorderlist(string orderInfoName)
        {
            var query = await _order.ToListAsync();
            if (orderInfoName != null)
            {
                query = query.Where(s=>s.OrderInfoName.Contains(orderInfoName)).ToList();
            }
            return ObjectMapper.Map<List<OrderInfo>, List<OrderDTO>>(query);
        }
        //修改订单
        [HttpPost,Route("UpOrderMessage")]
        public async Task<int> UpOrderMessage(OrderDTO dto)
        {
            OrderInfo order =await _order.GetAsync(dto.Id);
            order.OrderInfoName = dto.OrderInfoName;
            order.ShoppAddreTel = dto.ShoppAddreTel;
            
            return 1;
            



        }
    }
}
